package com.example.liuke.guangguang.base;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.Toast;

import com.example.liuke.guangguang.untils.CountTimer;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by liuke on 2017/11/16.
 */

public abstract class baseactivity extends AppCompatActivity {
    private CountTimer countTimerView;
    private Unbinder unbinder;
    //注意一封套在重写的时候重写protected这个oncreate否则在某些安卓版本下会报错
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(this.getLayoutId());
        Toast.makeText(this, "baseactivity启动", Toast.LENGTH_SHORT).show();
        EventBus.getDefault().register(this);
        unbinder = ButterKnife.bind(this);

        //初始化CountTimer，设置倒计时为5秒
       // countTimerView=new CountTimer(20000,1000,baseactivity.this);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
    }

    /**
     * 获取布局id
     *
     * @return
     */
    protected abstract int getLayoutId();

    private void timeStart() {
        new Handler(getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                countTimerView.start();
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            //获取触摸动作，如果ACTION_UP，计时开始。
            case MotionEvent.ACTION_UP:
               // countTimerView.start();
                break;
            //否则其他动作计时取消
            default:
                //countTimerView.cancel();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @Override
    protected void onPause() {
        super.onPause();
       // countTimerView.cancel();
    }

    @Override
    protected void onResume() {

        super.onResume();
       // timeStart();
    }
}
